import { _decorator, Component, Material, Node, renderer, Sprite, v2, v4, Vec2, Vec4 } from 'cc';
import GlobalPool from '../../Init/Tools/GlobalPool';
import { EDITOR } from 'cc/env';
const { ccclass, property, executeInEditMode } = _decorator;

// 自动播放帧动画 需要搭配 frameAnim-2d-effect使用
// 不能合批

@ccclass('FrameAnim2dCmp')
@executeInEditMode
export class FrameAnim2dCmp extends Component {

    @property(Vec2)
    animSize: Vec2 = v2(1, 1);

    @property
    stepTime = 0.1;

    @property
    isLoop = false;

    sprite: Sprite = null;
    pass: renderer.Pass = null;
    handle = 0;
    paramName = 'customUv';
    customUv = v4(1, 1, 0, 0);
    uvSpet = v2();
    uvIdx = v2();
    // 单次循环是否结束
    isRoundFinish = false;
    // 整个动画是否结束
    isAnimFinish = false;

    protected onLoad(): void {
        this.sprite = this.node.getComponent(Sprite);
        if (!EDITOR) {
            // 重新创建材质
            let mat = new Material();
            mat.copy(this.sprite.customMaterial);
            this.sprite.customMaterial = mat;
        }

        this.pass = this.sprite.customMaterial.passes[0];
        this.handle = this.pass.getHandle(this.paramName);

        // 计算uv步长
        this.uvSpet.x = 1 / this.animSize.x;
        this.uvSpet.y = 1 / this.animSize.y;
    }

    protected onEnable(): void {
        this.initUv();
    }

    protected onDisable(): void {
        this.resetUv();
    }

    protected update(dt: number): void {
        this.updateUv(dt);
    }

    //#region ---切换uv
    curt = 0;
    initUv() {
        this.curt = 0;
        this.uvIdx.set(0, 0);
        this.customUv.set(this.uvSpet.x, this.uvSpet.y, this.uvIdx.x * this.uvSpet.x, this.uvIdx.y * this.uvSpet.y);

        this.setUvParam();
        this.isRoundFinish = false;
        this.isAnimFinish = false;
    }
    resetUv() {

    }
    updateUv(dt) {
        if (this.isAnimFinish) return;

        this.curt += dt;
        if (this.curt >= this.stepTime) {
            this.curt = 0;

            // 切换uv
            this.uvIdx.x++;
            if (this.uvIdx.x >= this.animSize.x) {
                this.uvIdx.x = 0;
                this.uvIdx.y++;

                if (this.uvIdx.y >= this.animSize.y) {
                    this.uvIdx.y = this.isLoop ? 0 : this.animSize.y;
                    // 单次循环结束
                    this.isRoundFinish = true;
                    // console.log('单次循环结束' + this.customUv.z + '  ' + this.customUv.w);
                }
            }

            this.customUv.set(this.uvSpet.x, this.uvSpet.y, this.uvIdx.x * this.uvSpet.x, this.uvIdx.y * this.uvSpet.y);
            // console.log(this.customUv.z + '  ' + this.customUv.w);
            this.setUvParam();

            if (this.isRoundFinish) {
                if (!this.isLoop) {
                    this.isAnimFinish = true;
                    this.onUvFinish();
                    return;
                } else {
                    this.uvIdx.set(0, 0);
                    this.isRoundFinish = false;
                }
            }
        }
    }

    onUvFinish() {
        this.customUv.set(0, 0, 1, 1);
        this.setUvParam();
        // this.node.active = false;
        if (!EDITOR) {
            GlobalPool.put(this.node);
        }
        console.log('动画结束');
    }

    setUvParam() {
        this.pass.setUniform(this.handle, this.customUv);
    }

    //#endregion

}

